package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.PotentialCustomer;
import com.ruoyi.business.mapper.PotentialCustomerMapper;
import com.ruoyi.business.service.IPotentialCustomerService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 潜在客户Service业务层处理
 * 
 * @author wolfcode
 * @date 2021-07-26
 */
@Service
public class PotentialCustomerServiceImpl implements IPotentialCustomerService 
{
    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询潜在客户
     * 
     * @param id 潜在客户ID
     * @return 潜在客户
     */
    @Override
    public PotentialCustomer selectPotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.selectPotentialCustomerById(id);
    }

    /**
     * 查询潜在客户列表
     * 
     * @param potentialCustomer 潜在客户
     * @return 潜在客户
     */
    @Override
    public List<PotentialCustomer> selectPotentialCustomerList(PotentialCustomer potentialCustomer)
    {
        List<SysUserRole> sysUserRoles = sysUserService.selectUserRoleByUserId(ShiroUtils.getUserId());  
        List<PotentialCustomer> list = null;
        for (SysUserRole sysUserRole : sysUserRoles) {
            //循环获取用户角色信息并查询是店长还是业务员
            if(SecurityUtils.getSubject().hasRole("shopOwner") || SecurityUtils.getSubject().hasRole("admin")){
                //店长
                list = potentialCustomerMapper.selectPotentialCustomerList(potentialCustomer);
            }else if(SecurityUtils.getSubject().hasRole("salesman")){
                //业务员
                list = potentialCustomerMapper.selectPotentialCustomerByUserId(potentialCustomer,sysUserRole.getUserId());
            }
        }
        if (list == null){
            list = Collections.EMPTY_LIST;
        }
        return list;
    }

    /**
     * 新增潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPotentialCustomer(PotentialCustomer potentialCustomer)
    {
        //判断用户手机是否插入
        PotentialCustomer customer = potentialCustomerMapper.selectPotentialCustomerByPhone(potentialCustomer.getPhone());
        if(customer != null){
            //存在
            throw new RuntimeException("该手机用户已存在");
        }else{
            //不存在
            //插入录入人
            potentialCustomer.setFollowUpUser(ShiroUtils.getUserId());
            potentialCustomer.setCreateTime(DateUtils.getNowDate());
            return potentialCustomerMapper.insertPotentialCustomer(potentialCustomer);
        }

    }

    /**
     * 修改潜在客户
     * 
     * @param potentialCustomer 潜在客户
     * @return 结果
     */
    @Override
    public int updatePotentialCustomer(PotentialCustomer potentialCustomer)
    {
        return potentialCustomerMapper.updatePotentialCustomer(potentialCustomer);
    }

    /**
     * 删除潜在客户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerByIds(String ids)
    {
        return potentialCustomerMapper.deletePotentialCustomerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除潜在客户信息
     * 
     * @param id 潜在客户ID
     * @return 结果
     */
    @Override
    public int deletePotentialCustomerById(Long id)
    {
        return potentialCustomerMapper.deletePotentialCustomerById(id);
    }
}
